﻿/* 
This file is part of Ingo Karstein's Web based PowerShell Console project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://webpsconsole.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.IO;
using System.Text;

namespace ik.PowerShell.WebPSConsole
{
    /// <summary>
    /// Summary description for InputQueue
    /// </summary>
    public class InputQueue : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            Server s = (Server)context.Session["server"];
            if( s == null )
                return;

            var cmd = context.Request["cmd"];
            if( string.IsNullOrEmpty(cmd) )
                return;
            if( string.Compare(cmd, "scd", true) == 0 )
            {
                try
                {
                    int w = int.Parse(context.Request["w"]);
                    int h = int.Parse(context.Request["h"]);
                    InputObjectConsoleDim i = new InputObjectConsoleDim
                    {
                        Height = h,
                        Width = w
                    };
                    s.AddInputObject(i);
                    context.Response.Write("1");
                }
                catch
                {
                    context.Response.Write("0");
                }
                return;
            }

            if( string.Compare(cmd, "rtf", true) == 0 )
            {
                context.Response.ContentType = "Application/msword";
                context.Response.AppendHeader("content-disposition", "attachment; filename=WebPSConsole.rtf");
                context.Response.Write(s.RTF);
                context.Response.End();
                return;
            }

            if( string.Compare(cmd, "st", true) == 0 )
            {
                context.Response.Write(Statics.SessionTimeout - ( (int)s.LastInput.TotalMinutes ));
                context.Response.End();
                return;
            }

            if( string.Compare(cmd, "stt", true) == 0 )
            {
                TimeSpan r = s.LastInput;
                TimeSpan r2 = Statics.SessionTimeoutSpan - r;
                string col = "green";
                int time = (int)r.TotalMinutes;
                if( time >= Statics.SessionTimeoutWarning && time < Statics.SessionTimeout )
                {
                    col = "gold";
                }
                if( time >= Statics.SessionTimeout )
                {
                    col = "red";
                }

                context.Response.Write(string.Format(@"<span style=""color: {2}"">{0:d2}:{1:d2}</span>", (int)r2.TotalSeconds / 60, (int)r2.TotalSeconds % 60, col));
                context.Response.End();
                return;
            }

            if( string.Compare(cmd, "rur", true) == 0 )
            {
                if( s.IsInUserRequestMode )
                {
                    s.AddInputObject(new InputObjectRestoreUserRequest());
                    context.Response.Write("1");
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("0");
                    context.Response.End();
                }
                return;
            }

            if( string.Compare(cmd, "buf", true) == 0 )
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(@"
                    <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
                    <html xmlns=""http://www.w3.org/1999/xhtml"">
                    <head runat=""server"">
                        <script src=""jQuery/jquery-1.6.2.min.js"" type=""text/javascript""></script>
                        <script src=""jQuery/jquery-ui-1.8.15.custom.min.js"" type=""text/javascript""></script>
                        <script src=""jQuery/jquery.json-2.2.min.js"" type=""text/javascript""></script>
                        <script src=""WebPSConsole.js"" type=""text/javascript""></script>
                        <link href=""css/ui-lightness/jquery-ui-1.8.15.custom.css"" rel=""stylesheet"" type=""text/css"" />
                        <link href=""css/WebPSConsole.css"" rel=""stylesheet"" type=""text/css"" />
                    </head>
                    <body style=""background-color:black"">
                    ");
                sb.AppendLine(s.BufferToHtml());
                sb.AppendLine(@"</body></html>");
                context.Response.Write(sb.ToString());
                context.Response.End();
                return;
            }

            if( string.Compare(cmd, "bu", true) == 0 )
            {
                string buf = s.BufferToHtml();
                context.Response.Write(buf);
                context.Response.End();
                return;
            }

            if( string.Compare(cmd, "nes", true) == 0 )
            {
                context.Session.Abandon();
                context.Response.End();
            }

            if( string.Compare(cmd, "urr", true) == 0 )
            {
                try
                {
                    string correlationId = context.Request["cId"];
                    string data = context.Request["data"];

                    InputObjectUserResponse i = new InputObjectUserResponse
                    {
                        requestCorrelationId = correlationId,
                        data = data
                    };

                    s.AddInputObject(i);
                    context.Response.Write("1");
                }
                catch
                {
                    context.Response.Write("0");
                }
                return;
            }

            if( string.Compare(cmd, "cp", true) == 0 )
            {
                InputObjectPipeCancelEvent i = new InputObjectPipeCancelEvent();
                s.AddInputObject(i);

                return;
            }

            if( string.Compare(cmd, "sc", true) == 0 )
            {
                string command = context.Request.Form["data"];

                InputObject i = new InputObject();
                i.s = command;
                s.AddInputObject(i);

                return;
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}